package com.fenger.sys.service.impl;

import com.fenger.sys.contast.SysConstast;
import com.fenger.sys.domain.Permission;
//import com.fenger.sys.mapper.PermissionMapper;
import com.fenger.sys.mapper.PermissionMapper;
import com.fenger.sys.service.PermissionService;
import com.fenger.sys.utils.DataGirdView;
import com.fenger.sys.vo.PermissionVo;
import com.fenger.sys.vo.RoleVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {


    @Autowired
    PermissionMapper permissionMapper;
    @Override
    @Transactional(readOnly = true)
    public List<Permission> queryAllPermission(PermissionVo permissionVo) {
        return this.permissionMapper.queryAllPermission(permissionVo);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Permission> queryPermissionByUserid(PermissionVo permissionVo, Integer userid) {

        return this.permissionMapper.queryPermissionByUserid( permissionVo,  userid);
    }

    @Override
    public DataGirdView queryAllPermissionForPage(PermissionVo permissionVo) {
        Page<Object> page = PageHelper.startPage(permissionVo.getPage(), permissionVo.getLimit());
        List<Permission> permissions = this.permissionMapper.queryAllPermission(permissionVo);
        return new DataGirdView(SysConstast.SUCCESS_CODE,SysConstast.SUCCESS_MSG,page.getTotal(),permissions);
    }

    @Override
    public Integer queryMaxOrderNum() {

        return this.permissionMapper.queryMaxOrderNum();
    }

    @Override
    public void addPermission(PermissionVo permissionVo) {
        this.permissionMapper.insertSelective(permissionVo);
    }

    @Override
    public void updatePermission(PermissionVo permissionVo) {
        this.permissionMapper.updateByPrimaryKeySelective(permissionVo);
    }

    @Override
    public void deletePermission(Integer id) {
        this.permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Boolean checkHasChildren(Integer id) {
        return this.permissionMapper.queryCountChildrenByPid(id)>0;
    }

    @Override
    public List<Permission> queryPermissionByRoleId(Integer id) {
        return this.permissionMapper.queryPermissionByRoleId(id);
    }



    /**
     * 为角色分配权限 和菜单
     * @param roleVo
     * @return
     */
    @Override
    public DataGirdView dispatcherPermission(RoleVo roleVo) {

        try {
            //先从sys role permission 中删除角色 的所有权限

            this.permissionMapper.deleteByRolePermissionPrimaryKey(roleVo.getId());
            //再在sys role permission 中添加 权限
            Integer[] ids = roleVo.getIds();
            for (Integer id : ids) {
                this.permissionMapper.addRolePermission(roleVo.getId(),id);
            }
            return new DataGirdView(SysConstast.DISPATCHER_SUCCESS_CODE,SysConstast.DISPATCHER_SUCCESS_MSG,0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            return new DataGirdView(SysConstast.DISPATCHER_ERROR_CODE,SysConstast.DISPATCHER_ERROR_MSG,0L,null);
        }
    }

    @Override
    public List<String> queryPermissionByUseridForList(Integer id) {
        PermissionVo permissionVo = new PermissionVo();
        permissionVo.setType(SysConstast.TYPE_PERMISSION_PERMISSION);
        List<Permission> permissions = this.permissionMapper.queryPermissionByUserIdForList(permissionVo,id);
        List<String> list = new ArrayList<>();
        for (Permission permission : permissions) {
            list.add(permission.getPercode());
        }
        return list;
    }
}
